<?php
class ProductModel extends Model {
	protected $_validate = array(
	    array('title','require','请填写标题！'),
		
    );
	protected $_auto = array(
        array('datetime','time',1,'function'),
		array('update_time','time',2,'function'),
		array('update_time','time',1,'function'),
    );
	
	public function _list($view,$map,$sort='sort',$pagesize=20){
		//计算总页数
		$count=$this->where($map)->count();
		//实例化分页类
		import ( 'ORG.Util.Page' );
		$p=new Page($count,$pagesize);
		$list = $this->where($map)->order($sort)->limit($p->firstRow.',
'.$p->listRows)->select();
        $pages=$p->show('news');
		$view->assign('list',$list);
		$view->assign('pages',$pages);
	}
	//通过商品ID获取当前可选规格列表,返回未被选中的值
	public function getSKUListByIdStatus($cid){
		$attrarr = $ret = array();
		$attrModel=M('Product_attr');
		$valModel=M('Attr_val');
		$dataarr = explode(':', $cid);
		$id = $dataarr[0];
		if($dataarr[1] != ''){
			$attrarr = explode(';', $dataarr[1]);
		}
		
		//dump($dataarr[1]);
		$attrList=$attrModel->alias("a")->where(array('a.product_id'=>$id))->join("left join __ATTR__ as b on b.id=a.attr_id")->order("b.sort")->group('a.attr_id')->having("count(*)>0")->Field("b.id as id, b.title as title")->select();
		//echo $attrModel->getLastSql();
		foreach($attrList as $k=>$v){
			$attrList = $attrModel->alias('pa')->join("left join __ATTR_VAL__ as av on av.id=pa.attr_val_id")->where(array('av.attr_id'=>$v['id'],'pa.product_id'=>$id))->order('av.sort')->select();
			
			$pattr = array();
			foreach($attrList as $key => $val){
				$pattr[] = $val['attr_val_id'];
				
			}
			//dump($pattr);dump($attrarr);
			$isex = array_intersect($pattr, $attrarr);
			if(count($isex) == 0){
				$ret[] = $v['title'];
			}
		}
		return $ret;
	}
	//通过商品ID获取当前可选规格列表
	public function getSKUListById($id){
		$attrModel=M('Product_attr');
		$valModel=M('Attr_val');
		
		$attrList=$attrModel->alias("a")->where(array('a.product_id'=>$id))->join("left join __ATTR__ as b on b.id=a.attr_id")->order("b.sort")->group('a.attr_id')->having("count(*)>0")->Field("b.id as id, b.title as title")->select();
		//echo $attrModel->getLastSql();
		foreach($attrList as $k=>$v){
			$attrList[$k]['list']=$attrModel->alias('pa')->join("left join __ATTR_VAL__ as av on av.id=pa.attr_val_id")->where(array('av.attr_id'=>$v['id'],'pa.product_id'=>$id))->order('av.sort')->select();
		}
		if($attrList!==false){//dump($attrList);
			return $attrList;
		}else{
			return false;
		}
	}
	//通过商品ID获取当前SKU
	public function getSKUById($id){
		$stockModel=M('Product_stock');
		$stock=$stockModel->where(array('product_id'=>$id))->select();
		//dump($stock);
		//组合json数据
		$arr=array();
		foreach($stock as $k=>$v){
			if($v['stock']>0){
				$arrs['price']=$v['reprice'] == 0 ? $this-> where(array('id' => $v['product_id'])) -> getField('price') : $v['reprice'];
				$arrs['count']=(int)$v['stock'];
				$arr[$v['attrList']]=$arrs;
			}
		}
		return json_encode($arr);
	}
}

?>